unit FormMDIChild;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms,
  Vcl.Dialogs, Vcl.ExtCtrls, Vcl.Buttons,

  DatamoduleInterface, FormDevExpress,

  cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinscxPCPainter, dxLayoutContainer,
  dxLayoutControl, dxSkinsDefaultPainters;
  //cxGridStructureNavigator;

type
  TfmMDIChild = class(TfmDevExpress)
  protected
    procedure DoClose(var Action: TCloseAction); override;
  private
    procedure WMClose(var Message: TWMClose); message WM_CLOSE;
  public
    { Public declarations }
    constructor CreateAsMDIChild(AOwner: TComponent);
  end;

procedure CloseAllMDIChildWindows;

var
  fmMDIChild: TfmMDIChild;

implementation

{$R *.dfm}

procedure CloseAllMDIChildWindows;
var
  i: Integer;
begin
  if Assigned(Application.MainForm) then

    for i := 0 to Application.MainForm.MDIChildCount - 1 do
    begin
      if Assigned(Application.MainForm.MDIChildren[i]) then
        Application.MainForm.MDIChildren[i].Free;
    end;
end;

constructor TfmMDIChild.CreateAsMDIChild(AOwner: TComponent);
begin
  inherited Create(AOwner);
  self.FormStyle := fsMDIChild;
end;

procedure TfmMDIChild.DoClose(var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfmMDIChild.WMClose(var Message: TWMClose);
begin
  if Assigned(self) then
  begin
    inherited;

    if FormStyle = fsMDIChild then
      FreeAndNil(self);
  end;
end;

end.
